require(['../config'],function(){
    require(['jquery','template','header','bootstrap'],function($,Template){
        function Position(){
         this.array = null;
          this.init();
        }
        $.extend(Position.prototype,{
            init(){
                this.render();
                this.addListener();
            },
        
           //提交,修改删除绑定
            addListener(){
                $('.btnSub').on('click',this.postData.bind(this));
                $('.maxTable tbody').on('click','btnUpdeta',this.update);
                $('.maxTable tbody').on('click','btnDelete',this.delete);
                //分页
                $('.table-page-x').on('click','.a',function(e){
                  
                    $(e.target).parents('li').addClass('active').siblings('li').removeClass('active');
                   
                    
                    this.render($('.table-page .active>a').text())
             
            }.bind(this))
        },
           //模态框的submit
            postData:function(){
                let formData = new FormData($('#dialogForm').get(0));

				 
                $.ajax({
                        type:'post',
                        url:'/api/positions/add',
                        data:formData,
                        processData: false,
                        contentType: false,
                        context:this,
                        success:function(data){
                         //根据页码,返回整页数据
                            $("#addModal").modal('hide');
                            //成果后调用分页函数
                            this.render($('.table-page .active>a').text());
                    }
                })
            },
            delete(event){
                const src = event.target,
                   id = $(src).parents('tr').find('.id').text();
                array.filter(function(item){
                    //点击所在行的id
                    if(id==item.id){
                        return false;
                    }
                   
                })
            },
            update(){
                $("#dialogForm").modal('show');
                //填充数据到form的input中???
            },
            //分页以及渲染主体函数
            render(page){
               
                page =page||1;
                $.ajax({
                    url:'/api/positions/page',
                    type:'get',
                    data:{page:page},
                    success:function(data){
                        //tbody数据结构
                       
                        let main= data.res_body.main;
                        const mainhtml = Template("tbody-template", {main});
                       
                        $('.maxTable tbody').html(mainhtml);
                        //分页数据
                        var totalPage= data.totalPage;
                        totalPage = Template("pageTemplate",{totalPage});
                        $('.table-page-x').html(totalPage);
                        //加active
                        $(".table-page-x li").eq(data.currentPage).addClass('active');

                      
                     
                    }
                })

            }
            
        })
        return new Position()
    })
})
//$('.table-page .active>a').text()